package com.api.regression.group.getdocument;

import java.lang.reflect.Type;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.api.util.AssertUtil;
import com.api.util.DataUtil;
import com.google.gson.reflect.TypeToken;
import com.zyncro.api.IGroupApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.DocumentApi;
import com.zyncro.api.model.ObjectResultApi;

@ContextConfiguration(locations = { "classpath:/group/group-context.xml" })
public class GetDocumentOpenGroup extends AbstractApiTest {

	@Value("#{groupdocuments['user.getDocuments'].appId}")
	private String userUrn;

	@Value("#{groupdocuments['user.getDocuments'].password}")
	private String userPassword;

	@Value("#{groupdocuments['group.private'].urn}")
	private String groupUrn;

	@Value("#{groupdocuments['group.private'].documentFileUrn}")
	private String groupFileUrn;

	@Value("#{groupdocuments['group.private'].documentLinkUrn}")
	private String groupLinkUrn;

	@Value("#{groupdocuments['group.private'].documentShortcutUrn}")
	private String groupShortcutUrn;

	@BeforeClass
	protected void setParameters() throws Exception {

		getApi().login(userUrn, userPassword);
	}

	@Test
	public void testGetDocumentFile() throws ZyncroApiException {

		ObjectResultApi<DocumentApi> result = getApi().getService(IGroupApiService.class).getDocument(groupUrn, groupFileUrn);

		Type returnType = new TypeToken<ObjectResultApi<DocumentApi>>() {
		}.getType();
		ObjectResultApi<DocumentApi> expected = DataUtil.getDtoFromResource(this.getClass(), this.getClass().getSimpleName() + "File.json",
				returnType);

		AssertUtil.assertDTOEquals(result, expected);
	}

	@Test
	public void testGetDocumentLink() throws ZyncroApiException {

		ObjectResultApi<DocumentApi> result = getApi().getService(IGroupApiService.class).getDocument(groupUrn, groupLinkUrn);

		Type returnType = new TypeToken<ObjectResultApi<DocumentApi>>() {
		}.getType();
		ObjectResultApi<DocumentApi> expected = DataUtil.getDtoFromResource(this.getClass(), this.getClass().getSimpleName() + "Link.json",
				returnType);

		AssertUtil.assertDTOEquals(result, expected);
	}

	@Test
	public void testGetDocumentShortcut() throws ZyncroApiException {

		ObjectResultApi<DocumentApi> result = getApi().getService(IGroupApiService.class).getDocument(groupUrn, groupShortcutUrn);
		Type returnType = new TypeToken<ObjectResultApi<DocumentApi>>() {
		}.getType();
		ObjectResultApi<DocumentApi> expected = DataUtil.getDtoFromResource(this.getClass(), this.getClass().getSimpleName()
				+ "Shortcut.json", returnType);

		AssertUtil.assertDTOEquals(result, expected);
	}
}
